//
//  ViewController.m
//  ScrolBigViews
//
//  Created by wangyunpeng on 2016/11/18.
//  Copyright © 2016年 lanxum－mac. All rights reserved.
//

//对于展示的图片可以进行控件封装 点击事件 indexLab等（可扩展处）

#import "ViewController.h"

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width) //屏幕宽度
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) //屏幕高度
@interface ViewController ()<UIScrollViewDelegate>


@property(nonatomic,strong)UIScrollView *rootScrollView;

@property(nonatomic,assign)NSUInteger currentIndex;//从0开始

@property(nonatomic,strong)NSArray *imgNames;
@property(nonatomic,strong)NSMutableArray *imgViewsArray;//存放三个imgView
@end

@implementation ViewController
{
    CGFloat frginX;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _imgViewsArray=[[NSMutableArray alloc]init];
    _imgNames=[[NSArray alloc]initWithObjects:@"ez.png",@"jie.png",@"nj.png",@"xiazi.png", nil];
    self.navigationController.navigationBarHidden=YES;
    
    [self initUI];
    // Do any additional setup after loading the view, typically from a nib.
}
-(void)initUI{
    _currentIndex=0;
    
    _rootScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 140, SCREEN_WIDTH, 280)];
    _rootScrollView.backgroundColor=[UIColor redColor];
    _rootScrollView.pagingEnabled=YES;
    _rootScrollView.delegate=self;
    _rootScrollView.contentSize=CGSizeMake(SCREEN_WIDTH*4,0);
    [self.view addSubview:_rootScrollView];
    [self addImageArray];
}

-(void)addImageArray{

    for (NSUInteger i=0; i<3;i++) {
        UIImageView *imgView= [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, 280)];
        imgView.userInteractionEnabled=YES;
        imgView.tag=100+i;
        
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake((SCREEN_WIDTH*(i+1))-180, 200, 40, 40)];
        lab.text=[NSString stringWithFormat:@"%ld",i+1];
        lab.textColor=[UIColor blackColor];
        lab.backgroundColor=[UIColor blueColor];
        lab.tag=100+i;
        
        [_rootScrollView addSubview:imgView];
       // [_rootScrollView addSubview:lab];
        [_imgViewsArray addObject:imgView];

    }
    
    //scrollview滚动到中间位置
    _rootScrollView.contentOffset=CGPointMake(SCREEN_WIDTH, 0);
    [self loadData];
}


-(void)loadData{
   
    UIImageView *midImg=_imgViewsArray[1];//中间imageview
    midImg.image=[UIImage imageNamed:_imgNames[_currentIndex]];
    UIImageView *leftImg=_imgViewsArray[0];//左边imageview
    UIImageView *rightImg=_imgViewsArray[2];//右边imageview
    
    if (_currentIndex==0) {
        //第一张
        
        leftImg.image=[UIImage imageNamed:_imgNames[_imgNames.count-1]];
        rightImg.image=[UIImage imageNamed:_imgNames[_currentIndex+1]];//右边
    }else if (_currentIndex==_imgNames.count-1){
        //最后一张
        leftImg.image=[UIImage imageNamed:_imgNames[_currentIndex-1]];
        rightImg.image=[UIImage imageNamed:_imgNames[0]];//右边
    }else{
    
       // 其它
        leftImg.image=[UIImage imageNamed:_imgNames[_currentIndex-1]];
        rightImg.image=[UIImage imageNamed:_imgNames[_currentIndex+1]];//右边
    }
}





/**************scrollview delegate*****************/
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (frginX<scrollView.contentOffset.x) {
        NSLog(@"左划 +1");
        
        if (_currentIndex==_imgNames.count-1) {
            _currentIndex=0;
        }else{
           _currentIndex++;
        }
    }
    if (frginX>scrollView.contentOffset.x) {
        NSLog(@"右划 -1");
        if (_currentIndex==0) {
            _currentIndex=_imgNames.count-1;
        }else{
            _currentIndex--;
        }
    }
    scrollView.contentOffset=CGPointMake(SCREEN_WIDTH, 0);
    [self loadData];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    frginX=scrollView.contentOffset.x;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
